<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
	<head>
		<th:block th:include="include :: header('虚拟用况组 字段参数选择页面')" />
		<link th:href="@{/css/common.css}" rel="stylesheet"/>
		<link th:href="@{/js/plugins/swiper/swiper-bundle.css}" rel="stylesheet"/>
		<link th:href="@{/css/choose_data_map.css}" rel="stylesheet"/>
		<script th:src="@{/js/plugins/swiper/swiper-bundle.js}"></script>
	</head>
	<body>
	<form id="form-interface-field">
		<div class="wrapper wrapper-content field bff-field">

				<input type="hidden" name="interfaceId" th:value="${interfaceId}">
				<input type="hidden" name="isInParam" id="isInParam" th:value="${isInParam}">
				<input type="hidden" name="type" value="1">
				<input type="hidden" name="objectType" value="1">
				<input type="hidden" id="fieldInfo" name="fieldInfo">
				<div class="content">
					<ul class="tab-list" role="tablist">
					</ul>
					<div class="tab-content" id="tabDiv">
					</div>
				</div>

		</div>
	</form>
	</body>
	<th:block th:include="include :: footer" />
	<script th:inline="javascript">
		var prefix = ctx + "virtualUsageGroup/virtualUsageGroup";
		var initFlag = true;
		var virtualUsageList = [[${virtualUsageList}]] || [];
		var interfaceFieldList = [[${listFieldParam}]] || [];
		function submitHandler() {
			var count = $("#fieldSelected",window.parent.document).text();
			if (count > 0) {
				var config = {
					url: prefix + "/saveGroupParam",
					type: "post",
					async: false,
					dataType: "json",
					data: $('#form-interface-field').serialize(),
					beforeSend: function () {
						$.modal.loading("正在处理中，请稍后...");
						$.modal.disable();
					},
					success: function(result) {
						if (result.code == web_status.SUCCESS) {
							$.modal.close();
						} else if (result.code == web_status.WARNING) {
							$.modal.alertWarning(result.msg)
						}  else {
							$.modal.alertError(result.msg);
						}
						$.modal.closeLoading();
					}
				};
				$.ajax(config)
			} else {
				$.modal.alertWarning("请选择字段");
			}
		}

		$(function () {
			initUl(virtualUsageList);

			$("body").on('change', '.allSelect', function () {
				var flag = this.checked;
				$(this).parents("table").find("tbody input").each(function(){
					if (!$(this).attr('disabled')) {
						$(this).prop("checked", flag);
					}
				});

				checkboxClick();
			});
		})

		function initUl(virtualUsageList) {
			if (virtualUsageList && virtualUsageList.length > 0) {
				for (var i=0; i<virtualUsageList.length; i++) {
					var virtualUsageTpl = $("#virtualUsageTpl").html();
					var id = virtualUsageList[i].id;
					var slaveInterfaceName = virtualUsageList[i].slaveInterfaceName;
					laytpl(virtualUsageTpl).render(id, function (html) {
						$("#tabDiv").append(html);
					});
					if (i==0 && initFlag) {
						initFlag = false;
						$("ul").append("<li class='active'><a aria-expanded=\"true\"  href=\"#tab-"+id+"\" onclick=\"clickTab("+id+")\" role=\"tab\" data-toggle=\"tab\" style = \"margin-right: 15px;\">"+slaveInterfaceName+"</a></li>");
						$("#tab-" + id).addClass('active');
						clickTab(id);
					} else {
						$("ul").append("<li class=''><a aria-expanded=\"false\" href=\"#tab-"+id+"\" onclick=\"clickTab("+id+")\" role=\"tab\" data-toggle=\"tab\" style = \"margin-right: 15px;\">"+slaveInterfaceName+"</a></li>");
					}
				}
			}
            new Swiper(".tabSwiper", {
                centeredSlidesBounds: true,
                slidesPerView: "auto",
                spaceBetween: 0,
                grabCursor: true,
                slideToClickedSlide: true
            });
		}

		function search(id) {
			loadField(id, $("#tab-" + id + " #searchValue").val());
		}

		var record = {};

		function clickTab(id) {
			if (!record[id]) {
				loadField(id, $("#tab-" + id + " #searchValue").val());
				record[id] = true;
			}
		}

		function loadField(id, searchValue) {
			var config = {
				url: ctx + "interface/interface" + "/listInterfaceALlField",
				type: "post",
				dataType: "json",
				data: "type=3&interfaceId="+id+"&isInParam="+$("#isInParam").val()+"&searchValue="+searchValue,
				beforeSend: function () {
					$.modal.loading("正在处理中，请稍后...");
				},
				success: function(result) {
					$("#tab-" + id + " table tbody").empty();
					$.modal.closeLoading();
					if (result.code == web_status.SUCCESS) {
						if (result.data && result.data.length > 0) {
							var data = {};
							data.list = convert(result.data);
							var paramsTplHtml = $("#paramsTpl").html();
							laytpl(paramsTplHtml).render(data, function (html) {
								$("#tab-" + id + " table tbody").append(html);
							});

						}
					} else if (result.code == web_status.WARNING) {
						$.modal.alertWarning(result.msg)
					} else {
						$.modal.alertError(result.msg);
					}
				}
			};
			$.ajax(config);
		}

		function convert(data) {
			if (data && data.length > 0) {
				for (var i=0; i<data.length; i++) {
					for (var j=0; j<interfaceFieldList.length; j++) {
						if (interfaceFieldList[j].dataRelationType == data[i].dataRelationType
							&& interfaceFieldList[j].dataRelationId == data[i].id) {
							data[i].selected = true;
							break;
						}
					}
				}
			}
			return data;
		}

		function checkboxClick() {
			var count = 0;
			var value = '';
			$('table tbody').find(':checkbox').each(function(){
				if ($(this).is(":checked")) {
					count ++;
					value = value + $(this).val() + "=" + $(this).attr('data-dataRelationType') + ',';
				}
			});
			$("#fieldInfo").val(value);
			$("#fieldSelected",window.parent.document).text(count);
		}

	</script>
	<script id="paramsTpl" type="text/template">
		{{# layui.each(d.list, function(index, a){ }}
		<tr>
			{{# if(a.selected) { }}
			<td><input name="fieldId" disabled="disabled" onclick="checkboxClick()" type="checkbox" value="{{a.id}}"></td>
			{{# } else { }}
			<td><input name="fieldId" data-id="{{a.id}}" data-dataRelationType="{{a.dataRelationType}}" onclick="checkboxClick()" type="checkbox" value="{{a.id}}"></td>
			{{# } }}
			<td>
				{{a.fieldName}}
				{{# if(a.paramsName) { }}
				({{a.paramsName}})
				{{# } }}
			</td>
			<td>{{a.fieldEnName}}</td>
			<td>{{a.fieldDataType}}</td>
		</tr>
		{{# }); }}
	</script>
	<script id="virtualUsageTpl" type="text/template">
		<div id="tab-{{d}}" class="tab-pane">
			<div class="search-bg">
				<div data="{{d}}" onclick="search({{d}})" class="search-img searchDiv"></div>
				<input type="text" class="search" id="searchValue">
			</div>
			<table class="table table-hover" style = "margin-top:10px;">
				<thead>
				<tr>
					<td><input type="checkbox" class="allSelect"></td>
					<td>字段中文名称</td>
					<td>字段英文名称</td>
					<td>字段类型</td>
				</tr>
				</thead>
				<tbody>
				</tbody>
			</table>
		</div>
	</script>
</html>
